var express = require('express');
var router = express.Router();
var db = require('../config/mysql');

function filterMenu(data) {
  let parents = data.filter(item => item.parentId == 0);
  let childs = data.filter(item => item.parentId != 0);
  childs.forEach(c => {
    parents.forEach(p => {
      c.parentId == p.id && (p.children ? p.children.push(c) : p.children = [c]);
    })
  });
  return parents
}

// 获取路由列表
router.post('/getMenuTree', (req, res) => {
  let sql = `SELECT m.* FROM menu m`;
  db.query(sql, [], (reslute) => {
    let data = filterMenu(reslute);
    res.json({
      status: 200,
      message: '成功',
      data,
      timestamp: db.getTime()
    })
  })
})

// 新增菜单
router.post('/addRouters', (req, res) => {
  let { parentId, name, url, iconPath } = req.body;
  let sql = `SELECT * FROM menu WHERE url = ?`;
  db.query(sql, [url], (reslute) => {
    if(!reslute.length) {
      let sql = `INSERT INTO menu (parentId, name, url, iconPath) VALUES (?, ?, ?, ?)`;
      db.query(sql, [parentId, name, url, iconPath], (reslute) => {
        res.json({
          status: 200,
          message: '成功',
          data: reslute,
          timestamp: db.getTime()
        })
      })
    } else res.json({
      status: 300,
      message: '菜单url重复',
      data: null,
      timestamp: db.getTime()
    })
  })
})

// 更新菜单
router.post('/updateRouters', (req, res) => {
  let { name, url, iconPath, id } = req.body;
  let sql = `SELECT * FROM menu WHERE url = ? AND id != ?`;
  db.query(sql, [url, id], (reslute) => {
    if(!reslute.length) {
      let sql = `UPDATE menu SET name = ?, url = ?, iconPath = ? WHERE id = ?`;
      db.query(sql, [name, url, iconPath, id], (reslute) => {
        res.json({
          status: 200,
          message: '成功',
          data: reslute,
          timestamp: db.getTime()
        })
      })
    } else res.json({
      status: 300,
      message: '菜单url重复',
      data: null,
      timestamp: db.getTime()
    })
  })
})

// 删除菜单
router.post('/removeRouters', (req, res) => {
  let { id } = req.body;
  let sql = `DELETE FROM menu WHERE id = ?`;
  db.query(sql, [id], (reslute) => {
    res.json({
      status: 200,
      message: '成功',
      data: reslute,
      timestamp: db.getTime()
    })
  })
})

// router.post('/getPageButton', (req, res) => {
//   let sql = `SELECT * FROM btnAuth WHERE id = ?`;
//   db.query(sql, [], (reslute) => {
//     res.json({
//       status: 200,
//       message: '成功',
//       timestamp: db.getTime()
//     })
//   })
// })

// 获取选中菜单下按钮权限
router.post('/getMenuButtons', (req, res) => {
  let { menuId } = req.body;
  let sql = `SELECT * FROM btnauth WHERE menuId = ?`;
  db.query(sql, [menuId], (reslute) => {
    res.json({
      status: 200,
      message: '成功',
      data: reslute,
      timestamp: db.getTime()
    })
  })
})

// 新增菜单按钮
router.post('/addMenuButton', (req, res) => {
  let { buttonName, buttonKey, menuId } = req.body;
  let sql = `SELECT * FROM btnauth WHERE buttonKey = ? AND menuId = ?`;
  db.query(sql, [buttonKey, menuId], (reslute) => {
    if(!reslute.length) {
      let sql = `INSERT INTO btnauth (buttonName, buttonKey, menuId) VALUES (?, ?, ?)`;
      db.query(sql, [buttonName, buttonKey, menuId], (reslute) => {
        res.json({
          status: 200,
          message: '成功',
          data: reslute,
          timestamp: db.getTime()
        })
      })
    } else res.json({
      status: 300,
      message: '按钮Key重复',
      data: null,
      timestamp: db.getTime()
    })
  })
})

// 更新菜单按钮
router.post('/updateButton', (req, res) => {
  let { buttonName, buttonKey, menuId, id } = req.body;
  let sql = `SELECT * FROM btnauth WHERE buttonKey = ? AND menuId = ? AND id != ?`;
  db.query(sql, [buttonKey, menuId, id], (reslute) => {
    if(!reslute.length) {
      let sql = `UPDATE btnauth SET buttonName = ? WHERE buttonKey = ? AND menuId = ?`;
      db.query(sql, [buttonName, buttonKey, menuId], (reslute) => {
        res.json({
          status: 200,
          message: '成功',
          data: reslute,
          timestamp: db.getTime()
        })
      })
    } else res.json({
      status: 300,
      message: '按钮Key重复',
      data: null,
      timestamp: db.getTime()
    })
  })
})

// 删除菜单按钮
router.post('/deleteMenuButton', async (req, res) => {
  let { buttonKey, menuId } = req.body;
  let sql1 = `DELETE FROM btnauth WHERE buttonKey = ? AND menuId = ?`;
  let sql2 = `DELETE FROM rolebtn WHERE buttonKey = ? AND menuId = ?`;
  let res1 = await db.query(sql1, [buttonKey, menuId])
  db.query(sql2, [buttonKey, menuId, res1], (reslute) => {
    res.json({
      status: 200,
      message: '成功',
      data: null,
      timestamp: db.getTime()
    })
  })
})

module.exports = router;